package com.softserveinc.settings;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.softserveinc.R;
import com.softserveinc.network.BitmapManager;

public class HardwareManager {

	private static final String FILE_DATA_SCHEME = "file";

	private final Context context;

	private Boolean isNetworkConnected;

	public HardwareManager(final Context context) {
		this.context = context;

		isNetworkConnected = checkNetworkConnection();
	}

	public void registerReceivers() {
		isNetworkConnected = checkNetworkConnection();

		context.registerReceiver(mConnReceiver, new IntentFilter(
				ConnectivityManager.CONNECTIVITY_ACTION));

		IntentFilter filter = new IntentFilter();

		filter.addAction(Intent.ACTION_MEDIA_REMOVED);
		filter.addAction(Intent.ACTION_MEDIA_CHECKING);
		filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		filter.addAction(Intent.ACTION_MEDIA_SHARED);
		filter.addDataScheme(FILE_DATA_SCHEME);

		context.registerReceiver(mExternalStorageReceiver, filter);
	}

	public boolean isNetworkConnected() {
		if (isNetworkConnected == null) {
			isNetworkConnected = checkNetworkConnection();
		}

		return isNetworkConnected;
	}

	private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(final Context context, final Intent intent) {
			isNetworkConnected = checkNetworkConnection();

			String message;
			if (isNetworkConnected) {
				message = context.getString(R.string.internet_connection);
			} else {
				message = context.getString(R.string.no_internet_connection);
			}

			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

			Log.i("broadcast", "connection avail " + isNetworkConnected);
		}
	};

	private BroadcastReceiver mExternalStorageReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(final Context context, final Intent intent) {
			String action = intent.getAction();

			boolean isSdMounted = true;
			if (action.equals(Intent.ACTION_MEDIA_REMOVED)
					|| action.equals(Intent.ACTION_MEDIA_SHARED)) {
				isSdMounted = false;
			} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
				isSdMounted = true;
			}

			Toast.makeText(context, action, Toast.LENGTH_SHORT).show();

			if (isSdMounted) {
				BitmapManager.copyCacheToExternalFolder(context);
			}
		}
	};

	private boolean checkNetworkConnection() {
		boolean isOnline;
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo i = cm.getActiveNetworkInfo();
		if (i == null) {
			isOnline = false;
		} else if (!i.isConnected()) {
			isOnline = false;
		} else if (!i.isAvailable()) {
			isOnline = false;
		} else {
			isOnline = true;
		}
		return isOnline;
	}
}
